<template>
	<view>
		<view class="search-box">
			<uni-search-bar :radius="100" cancelButton="none" :focus="true" v-model="searchValue" @input="input">
			</uni-search-bar>
		</view>
		<!-- 搜索列表 -->
		<view class="seach-list" v-if="searchReault.length!=0">
			<view class="seach-item" v-for="item in searchReault" :key="item.goods_id"
				@click="gotoDetail(item.goods_id)">
				<view class="goods-name">
					{{item.goods_name}}
				</view>
				<uni-icons type="arrowright" size="16"></uni-icons>
			</view>
		</view>
		<!-- 搜索历史 -->
		<view class="history-box" v-else>
			<!-- 标题区域 -->
			<view class="history-title">
				<text>搜索历史</text>
				<uni-icons type="trash-filled" size="17" @click="clear"></uni-icons>
			</view>
			<!-- 列表区域 -->
			<view class="history-list">
				<uni-tag v-for="(item,index) in history" :key="index" :text="item" @click="gotoSearchList(item)"></uni-tag>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				searchValue: '',
				// 防抖设置的定时器的id
				timer: null,
				// 搜索框
				kw: '',
				// 搜索结果列表
				searchReault: [],
				// 搜索历史列表
				historyList: []
			};
		},
		onLoad(){
			this.historyList=JSON.parse(uni.getStorageSync('kw'||[]))
		},
		methods: {
			// 输入搜索词
			input(e) {
				clearTimeout(this.timer)
				this.timer = setTimeout(() => {
					this.kw = e
					this.getsearchReault()
					// 更新搜索词
					this.updateHistory()
				}, 500)
			},
			// 获取搜索列表
			async getsearchReault() {
				if (this.kw.length == 0) {
					this.searchReault = []
					return
				}
				let {
					data: res
				} = await uni.$http.get('/api/public/v1/goods/qsearch', {
					query: this.kw
				})
				if (res.meta.status == 200) {
					this.searchReault = res.message
				} else uni.$showMsg()
			},
			// 点击跳转商品详情
			gotoDetail(id) {
				uni.navigateTo({
					url: '/subpkg/goods_detail/goods_detail?goods_id=' + id
				})
			},
			// 更新搜索词
			updateHistory() {
				// 对搜索词去重
				const set = new Set(this.historyList)
				set.delete(this.kw)
				set.add(this.kw)
				this.historyList = Array.from(set)
				// 将搜索历史存储到本地，持久化存储
				uni.setStorageSync('kw',JSON.stringify(this.historyList))
			},
			// 点击清空搜索历史
			clear() {
				this.historyList = []
				uni.removeStorageSync('kw')
			},
			// 点击搜索历史词进入搜索列表
			gotoSearchList(item){
				uni.navigateTo({
					url:'/subpkg/goods_list/goods_list?query='+item
				})
			}
		},
		computed: {
			// 对数组反转，搜索的新词放在最前面
			history() {
				return [...this.historyList].reverse()
			}
		}
	}
</script>

<style lang="scss">
	body {
		background-color: #fff;
	}

	.search-box {
		background-color: #c00000;
		position: sticky;
		top: 0;
		z-index: 99;
	}

	.seach-list {
		padding: 0 10rpx;

		.seach-item {
			display: flex;
			flex-wrap: nowrap;
			justify-content: space-between;
			padding: 24rpx 0;
			border-bottom: 1rpx solid #efefef;
		}

		.goods-name {
			width: 95%;
			text-overflow: ellipsis;
			overflow: hidden;
			display: inline-block;
			white-space: nowrap;
		}
	}

	.history-box {
		.history-title {
			display: flex;
			justify-content: space-between;
			height: 70rpx;
			line-height: 70rpx;
			font-size: 26rpx;
			border-bottom: 2rpx solid #efefef;
		}

		.uni-tag {
			margin: 10rpx 10rpx 0 0;
		}
	}
</style>
